# CPU设计和分布式AI中的流水线思想
import argparse
import os

import shutil
import json
from py2neo import Graph, Relationship, Node
from colorama import Back, Style

BLOG_ROOT = "./data/blog"
BLOG_CACHE_ROOT = os.path.join(BLOG_ROOT, "cache")
LOG_CONFIG_FILE = "./config/neo4j.json"
meta_path = os.path.join(BLOG_ROOT, "meta.json")

def update_one_file(path):
    print('debug {}'.format(path))
    assert os.path.exists(path), f'{path} 不存在！'
    os.system("scp -r \"{}\" root@101.43.239.71:/usr/local/go/ws/DustBE/data/blog".format(path))


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--blog_path', type=str, help='需要的文章标题')
    args = parser.parse_args()

    blog_path = args.blog_path

    assert os.path.exists(blog_path), f'源文件路径 {blog_path} 不存在！'
    file_name: str = os.path.basename(blog_path)
    title = file_name.split('.')[0]

    with open(blog_path, 'r', encoding='utf-8') as fp:
        count = 0
        for line in fp:
            count += 1
            if count == 2:
                break
    
    new_image_url = line

    # 修改meta文件
    with open(meta_path, 'r', encoding='utf-8') as fp:
        meta = json.load(fp)
    
    assert title in meta['index']
    index = meta['index'][title]
    meta['blog'][index]['imgUrl'] = new_image_url

    with open(meta_path, 'w', encoding='utf-8') as fp:
        json.dump(obj=meta, fp=fp, indent=4, ensure_ascii=False)
    
    # 偷个懒
    update_one_file(blog_path)

    # 修改数据库
    # 登录数据库
    with open(LOG_CONFIG_FILE, "r", encoding="utf-8") as fp:
        c = json.load(fp)
    config = {
        "profile" : "http:" + c["url"].split(":")[1] + ":8002",
        "name" : c["name"],
        "password" : c["password"]
    }
    g = Graph(**config)

    g.run('match (n:Blog) where n.name="{}" set n.imgUrl="{}";'.format(
        title, new_image_url
    ))